package com.example.mycontacts;

import java.util.StringTokenizer;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ViewContact extends Activity {	
	private String userid;
	private String addressIntent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_contact);
		Intent intent = getIntent();
		String numStr = intent.getStringExtra("userid");
		userid = numStr;
		integrateData(numStr);
		
		final Button button_cancel = (Button) findViewById(R.id.button_cancel);
        button_cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	//refreshes the mainScreen
            	Intent intent = new Intent(ViewContact.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);    
                startActivity(intent);
            	
            }
        });
		final Button button_edit = (Button) findViewById(R.id.button_edit);
        button_edit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
Intent intent = new Intent(ViewContact.this, EditExistingContact.class);
            	
            	
            	TextView contactName = (TextView)findViewById(R.id.contactName);
                intent.putExtra("contactName", contactName.getText().toString());
                intent.putExtra("cityStateZip", addressIntent);
                
                TextView phoneNumber = (TextView) findViewById(R.id.phoneNumber);
                intent.putExtra("phoneNumber", phoneNumber.getText().toString());
                
                TextView altPhoneNumber = (TextView) findViewById(R.id.altPhoneNumber);
                intent.putExtra("altPhoneNumber", altPhoneNumber.getText().toString());
                
                TextView email = (TextView) findViewById(R.id.email);
                intent.putExtra("email", email.getText().toString());
     
                TextView comments = (TextView) findViewById(R.id.comments);
                intent.putExtra("comments", comments.getText().toString());
                
                TextView category = (TextView) findViewById(R.id.category);
                intent.putExtra("category", category.getText().toString());
                
                intent.putExtra("userid", userid);
                
                startActivity(intent);
            }
        });
	
	}
	
	private void integrateData(String userid)
	{
		DBAdapter db = new DBAdapter(ViewContact.this);
		db.open();
		int x = Integer.parseInt(userid);
		Information info= db.getInformation(x);
		
		
		TextView contactName = (TextView) findViewById(R.id.contactName);
		contactName.setText(info.getName());	
		
		TextView category = (TextView) findViewById(R.id.category);
		category.setText(info.getCategory());
		
		addressIntent = info.getAddress();
		
		TextView cityStateZip = (TextView) findViewById(R.id.cityStateZip);
		String str = info.getAddress();
		String [] strArray = new String[4];
		StringTokenizer token = new StringTokenizer(str, "~|~");
		for(int i=0; token.hasMoreElements(); i++)	
		{
			strArray[i] = token.nextToken();
		}
		String output = "";
		for(int i=0; i<4; i++)
		{
			if(strArray[i] != null)
				output = output + strArray[i] + " ";
		}
		cityStateZip.setText(output);
		
		TextView phoneNumber = (TextView) findViewById(R.id.phoneNumber);
		phoneNumber.setText(info.getPhoneNumber());
		
		TextView altPhoneNumber = (TextView) findViewById(R.id.altPhoneNumber);
		altPhoneNumber.setText(info.getAltPhone());
		
		TextView email = (TextView) findViewById(R.id.email);
		email.setText(info.getEmailAddress());
		
		TextView comments = (TextView) findViewById(R.id.comments);
		comments.setText(info.getComments());
	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_view_contact, menu);
		return true;
	}
	
}
